import os
from pathlib import Path
import flightData.data.twenty_eight as f_data


class LabelsPathErrorException(Exception):
    def __init__(self, error_reason='path error'):
        Exception.__init__(self, error_reason)


def load_image_uv(time):
    # root_file = Path(__file__).resolve().parents[0]
    # s = '0_' + str(time)
    # labels_path = str(root_file) + label_dir

    target_path = f_data.get_label_dir(time)
    file = open(target_path, mode='r')
    uv_arr = []
    dx = 0.00000128
    dy = 0.00000128
    for line in file:
        uv = check_uv(line)
        uv_arr.append(uv)

    if len(uv_arr) == 1:
        return [(uv_arr[0][0] - 0.5) * 4056 * dx, (0.5 - uv_arr[0][1]) * 2280 * dy]
    else:
        for i in range(len(uv_arr)):
            if uv_arr[i][1] in [0.460526, 0.389474, 0.373246, 0.803509]:
                return [(uv_arr[i][0] - 0.5) * 4056 * dx, (0.5 - uv_arr[i][1]) * 2280 * dy]

    file.close()
    return []


def check_uv(s):
    x1, y1 = 0.0, 0.0
    s_list = s.split(' ')
    if s_list[0] == '0':
        x1 = float(s_list[1])
        y1 = float(s_list[2])
    else:
        print("出现非0标签")

    return [x1, y1]
